package com.demo.controller;

import com.demo.dto.LoginFormDTO;
import com.demo.dto.PasswordLoginFormDTO;
import com.demo.dto.Result;
import com.demo.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Api(tags = "登录模块")
@RestController
@RequestMapping("/login")
public class LoginController {
    @Resource
    LoginService loginService;

    @ApiOperation("短信验证码")
    @GetMapping("/common/code")
    Result<Void> code(@RequestParam("phone") String phone) {
        return loginService.verifyCode(phone);
    }

    @ApiOperation("短信验证码登录与注册")
    @PostMapping("/common/loginAndRegister")
    Result<String> loginAndRegisterByPhoneAndCode(@RequestBody LoginFormDTO loginFormDTO) {
        // 返回token
        return loginService.loginAndRegister(loginFormDTO);
    }

    @ApiOperation("图片验证码")
    @GetMapping("/common/imageCode")
    void imageCode(HttpServletResponse response, HttpSession session) {
        loginService.imageCode(response, session);
    }

    @ApiOperation("手机号，密码，图片验证码登录")
    @PostMapping("/common/loginByPassword")
    Result<String> loginByPassword(@RequestBody PasswordLoginFormDTO loginFormDTO, HttpSession session) {
        return loginService.loginByPassword(loginFormDTO, session);
    }

    @ApiOperation("手机号，密码注册")
    @PostMapping("/common/registerByPassword")
    Result<String> registerByPassword(@RequestBody PasswordLoginFormDTO loginFormDTO, HttpSession session) {
        return loginService.registerByPassword(loginFormDTO, session);
    }
}
